using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Zune.Forms
{
    public class Timer
    {
        #region Public Events
        /// <summary>
        /// Fires when Timer's interval is hit.
        /// </summary>
        public event EventHandler Tick;
        #endregion

        #region Private Members
        protected System.Threading.Timer _timer;
        private int _interval = System.Threading.Timeout.Infinite;
        private bool _enabled = false;
        #endregion

        #region Constructors
        public Timer()
        {
            _timer = new System.Threading.Timer(_timer_Tick, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
        }

        public Timer(int interval)
        {
            _interval = interval;
            _timer = new System.Threading.Timer(_timer_Tick, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
        }
        #endregion

        #region Event Handlers
        protected void _timer_Tick(object obj)
        {
            if (Tick != null)
                Tick(this, EventArgs.Empty);
        }
        #endregion

        #region Private Methods
        private void SetTimer()
        {
            if (_enabled)
            {
                _timer.Change(_interval, _interval);
            }
            else
            {
                _timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
            }
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets whether the Timer is enabled or not.
        /// </summary>
        public bool Enabled
        {
            get { return _enabled; }
            set 
            { 
                _enabled = value;
                SetTimer();
            }
        }

        /// <summary>
        /// Gets or sets the Interval.
        /// </summary>
        public int Interval
        {
            get { return _interval; }
            set 
            { 
                _interval = value;
                SetTimer();
            }
        }
        #endregion
    }
}